package com.mobile.collect.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.mobile.collect.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListViewItemClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
import cn.droidlover.xdroidmvp.kit.NToast;
import rx.Subscription;

/**
 * 类名: InputDialog
 * 作者: 李学伟
 * 日期: 2020年09月27日 15:04
 * 描述:
 */
public class InputDialog extends Dialog {
    Activity context;
    TextView leftBtn;
    TextView rightBtn;
    TextView inputEt;
    TextView labelTv;
    TextView cancelTv;
    String label;
    String oldTxt;
    int maxCount;
    Subscription subscription;
    boolean clickBack;
    InputCompleteListener completeListener;

    RecyclerView numberView;
    BaseRecyclerAdapter<String> numberAdapter;

    public static String[] broad0 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0"};
    public static String[] broad1 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "00"};
    public static String[] broad2 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."};
    public static String[] broad3 = new String[]{"A", "B", "C", "D", "E", "F", "K", "P", "R", "S", "T", "V", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "00", "000", ".", "-", "/"};
    public static String[] broad5 = new String[]{"1", "1.5", "2", "2.5", "3", "3.5", "4", "4.5", "5", "6", "7", "8", "9", "0", "."};
    public static String[] broad6 = new String[]{"0", "10", "15", "20", "25", "40", "50", "65", "80", "100", "150", "200", "250", "300", "350", "400", "450", "500", "600", "700", "800", "900", "1000"};

    public InputDialog(@NonNull Context context, String label, String oldTxt, InputCompleteListener listener, int layoutId, int maxCount) {
        super(context, R.style.WinDialog);
        setContentView(layoutId);
        this.context = (Activity) context;
        this.label = label;
        this.oldTxt = oldTxt;
        this.completeListener = listener;
        this.maxCount = maxCount;
        initViews();
    }

    public InputDialog(@NonNull Context context, String label, InputCompleteListener listener, int layoutId, int maxCount) {
        super(context, R.style.WinDialog);
        setContentView(layoutId);
        this.context = (Activity) context;
        this.label = label;
        this.oldTxt = oldTxt;
        this.completeListener = listener;
        this.maxCount = maxCount;
        initViews();
    }

    public InputDialog(@NonNull Context context, String label, InputCompleteListener listener, int maxCount) {
        super(context, R.style.WinDialog);
        setContentView(R.layout.dialog_number);
        this.context = (Activity) context;
        this.label = label;
        this.oldTxt = oldTxt;
        this.completeListener = listener;
        this.maxCount = maxCount;
        initViews();
    }


    protected void initViews() {
        rightBtn = findViewById(R.id.rightBtn);
        leftBtn = findViewById(R.id.leftBtn);
        inputEt = findViewById(R.id.inputEt);
        labelTv = findViewById(R.id.labelTv);
        numberView = findViewById(R.id.numberView);
        labelTv.setText(label);
        leftBtn.setText("取消");

        cancelTv = findViewById(R.id.cancelTv);
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onComplete();
            }
        });
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        numberAdapter = new BaseRecyclerAdapter<String>(context) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_latters;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                holder.setText(R.id.valueTv, getItemByPosition(position));
            }
        };
        numberView.setLayoutManager(new GridLayoutManager(context, 4));
        numberView.setAdapter(numberAdapter);
        numberAdapter.setItemClickListener(new ListViewItemClickListener() {
            @Override
            public void itemClick(int position) {
                if (position == numberAdapter.getItemCount() - 1) {
                    del();
                } else {
                    if (clickBack) {
                        completeListener.onInputComplete(numberAdapter.getItemByPosition(position));
                        dismiss();
                    } else {
                        appendText(numberAdapter.getItemByPosition(position));
                    }
                }

            }
        });
    }

    protected void onComplete() {
        String input = inputEt.getText().toString();
        if (!TextUtils.isEmpty(input)) {
            completeListener.onInputComplete(input);
        }
        dismiss();
    }

    protected void appendText(String value) {
        inputEt.append(value);
        String inputValue = inputEt.getText().toString();
        if (inputValue.length() > maxCount && maxCount != 0) {
            inputValue = inputValue.substring(0, maxCount);
            inputEt.setText(inputValue);
            NToast.showToast(getContext(), "最多" + maxCount + "字符", 1);
        }
    }

    public interface InputCompleteListener {
        void onInputComplete(String value);
    }

    public void del() {
        String input = inputEt.getText().toString();
        if (input.length() != 0) {
            inputEt.setText(input.substring(0, input.length() - 1));
        }
    }

    public void disPlay(String[] paramers) {
        disPlay(paramers, false);
    }

    public void disPlay(String[] paramers, boolean clickBack) {
        this.clickBack = clickBack;
        List<String> values = new ArrayList<>();
        values.addAll(Arrays.asList(paramers));
        values.add("删除");

        numberAdapter.addList(values);
        super.show();
    }
}
